import { Module } from 'vuex'
import { RootState } from '@/store/types'
import { getDataList as fetchMenusList } from '@/api/system/menu'

export interface RoleModuleState {
  menus: any[]
}

const roleModule: Module<RoleModuleState, RootState> = {
  namespaced: true,
  state: () => {
    return {
      menus: []
    }
  },
  getters: {
    menus(state) {
      return state.menus
    }
  },
  mutations: {
    changeMenus(state, payload) {
      state.menus = payload
    }
  },
  actions: {
    async init({ commit }) {
      // 请求完整菜单信息
      const menus = (await fetchMenusList())?.data?.list ?? []
      commit('changeMenus', menus)
    }
  }
}

export default roleModule
